<?php
/**
 * Created by PhpStorm.
 * User: yuchong
 * Date: 15-4-28
 * Time: 下午3:43
 */
class Article_model extends MY_Model {

    public function __construct() {
        $this->_table = 'db_article';
        parent::__construct();
    }


    /**
     * 获取单个文章信息
     * author:yuchong
     */
    public function getOneArticle($id){
        $query = $this->db->get_where($this->_table, array('id' => $id));
        $res = $query->row_array();
        return $res ? $res : false;
    }

    /**
     *    author:yuchong
    *     获取某个类型的文章
     */
    public function getTypeCommend($typeid){
        if($typeid){
            $return = $this->db->from($this->_table)
                                ->where("typeid", $typeid)
                                ->order_by("pubtime", "desc")
                                ->limit(6)
                                ->get()
                                ->result_array();
            return $return ? $return : false;
        }
        return false;
    }


    /**
     *    author:yuchong
     *    首页中间部位的推荐文章
     */
    public function getIndexCommend(){
        $return = $this->db->from($this->_table)
                            ->like("flag", 'a')
                            ->order_by("pubtime", "desc")
                            ->limit(13)
                            ->get()
                            ->result_array();
        return $return ? $return : false;
    }


    /**
     *    author:yuchong
     *    列表部位的推荐文章左侧
     */
    public function getListCommend(){
        $return = $this->db->from($this->_table)
            ->like("flag", 'a')
            ->order_by("pubtime", "desc")
            ->limit(10)
            ->get()
            ->result_array();
        return $return ? $return : false;
    }

    /**
     *    author:yuchong
     *    内容页的推荐文章底部
     */
    public function getContCommend($id, $typeid){
        if($typeid){
            $return = $this->db->select("id, title")
                ->from($this->_table)
                ->where("id !=", $id)
                ->where("typeid", $typeid)
                ->order_by("pubtime", "desc")
                ->limit(2)
                ->get()
                ->result_array();
            return $return ? $return : false;
        }
        return false;
    }


    /**
     *    author:yuchong
     *    获取列表页信息
     */
    public function getList($typeid, $page = 1, $limit = 10){
        $typeid && $this->db->where('typeid', $typeid);
        $this->db->order_by("pubtime desc");
        $this->db->select('*')->from($this->_table);
        $key = md5(microtime().'5tgame');
        $db[$key] = clone($this->db);
        $data['total'] = $db[$key]->count_all_results();
        $this->db->limit($limit, ($page - 1) * $limit);
        $data['result'] = $this->db->get()->result_array();
        if ($data['total'] && $data['result']) {
            return $data;
        }
        return false;
    }

    /**
     *    author:yuchong
     *    获取列表页信息
     */
    public function getTypeIds($typeid){
        $this->db->select("*");
        $this->db->where(array('typeid'=>$typeid));
        $this->db->order_by('id', 'desc');
        $query = $this->db->get($this->_table);
        if ($query->num_rows() > 0){
            return $query->result_array();
        }else{
            return false;
        }
    }




}